{extend name="$_admin_base_layout" /} {block name="body"}
<div class="container-fluid" style="padding-top: 10px; background-color: rgb(240, 242, 245);">
    <div class="panel">
        <form id="form-table" method="post" action="{:url('backup')}">
            <!-- 数据表格 -->
            <table class="table table-sorter">
                <!-- 表头 -->
                <thead>
                <tr>
                    <th style="width: 28px;"><input class="check-all" checked="chedked" type="checkbox" value=""></th>
                    <th>表名</th>
                    <th>数据量</th>
                    <th>数据大小</th>
                    <th>创建时间</th>
                    <th style="width:120px;">备份状态</th>
                    <th style="width: 80px;">操作</th>
                </tr>
                </thead>
                <!-- 列表 -->
                <tbody>
                {notempty name="data_list"} {volist name="data_list" id="table"}
                <tr>
                    <td class="num"><input class="ids" checked="chedked" type="checkbox" name="tables[]"
                                           value="{$table.name}"></td>
                    <td>{$table.name}</td>
                    <td>{$table.rows}</td>
                    <td>{$table.data_length|format_bytes}</td>
                    <td>{$table.create_time}</td>
                    <td class="info">未备份</td>
                    <td><a class="ajax-get" href="{:url('optimize?tables='.$table['name'])}">优化</a> <a class="ajax-get"
                                                                                                       href="{:url('repair?tables='.$table['name'])}">修复</a>
                    </td>
                </tr>
                {/volist} {else/}
                <tr>
                    <td class="text-center" colspan="7">aOh! 暂时还没有内容!</td>
                </tr>
                {/notempty}
                </tbody>

            </table>
        </form>
    </div>
</div>
{/block}
{block name="sub_menu_action"}
<ul class="nav nav-pills" style="margin-right: 10px;">

    <li><a id="btn-backup" class="btn btn-link" href="javascript:;"> <i class="icon icon-stack"></i> 立即备份</a></li>
    <li><a id="btn-optimize" class="btn btn-link" href="{:url('optimize')}"> <i class="icon icon-bullseye"></i> 优化表</a></li>
    <li><a id="btn-repair" class="btn btn-link" href="{:url('repair')}"> <i class="icon icon-wrench"></i> 修复表</a></li>
    <li><a class="btn btn-link" href="{:url('index2')}" data-toggle="modal" data-size="lg" data-icon="reply"> <i class="icon icon-reply-all"></i> 还原数据</a></li>

</ul>
{/block}
{block name="script"}
<script>
    $(function () {

        var $form = $("#form-table"), $export = $("#btn-backup"), tables
        $optimize = $("#btn-optimize"), $repair = $("#btn-repair");

        $optimize.add($repair).click(function () {
            $(this).parents('ul').find('a').addClass("disabled");
            $.post(this.href, $form.serialize(), function (data) {
                if (data.code) {

                    var msg = new $.zui.Messager(data.msg, {
                        placement: 'top',
                        type: 'success'
                    }).show();
                } else {

                    var msg = new $.zui.Messager(data.msg, {
                        placement: 'top',
                        type: 'error'
                    }).show();

                }
                ;
                $optimize.parents('ul').find('a').removeClass("disabled");

            }, "json");
            return false;
        });

        $export.click(function () {

            $export.parents('ul').find('a').addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post($form.attr("action"), $form.serialize(), function (data) {
                if (data.code) {
                    tables = data.data.tables;
                    $export.html(data.msg + "开始备份，请不要关闭本页面！");
                    backup(data.data.tab);
                    window.onbeforeunload = function () {
                        return "正在备份数据库，请不要关闭！"
                    }
                } else {

                    var msg = new $.zui.Messager(data.msg, {
                        placement: 'top',
                        type: 'danger'
                    }).show();
                    $export.parents('ul').find('a').removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(
                        function () {

                            $(this).removeClass('disabled').prop('disabled', false);
                        }, 1500);
                }
            }, "json");
            return false;
        });

        function backup(tab, code) {
            code && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function (data) {
                if (data.code) {
                    showmsg(tab.id, data.msg);
                    if (!$.isPlainObject(data.data.tab)) {
                        $export.parents('ul').find('a').removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function () {
                            return null;
                        }
                        return;
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {

                    var msg = new $.zui.Messager(data.msg, {
                        placement: 'top',
                        type: 'danger'
                    }).show();
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(
                        function () {
                            $(that).removeClass('disabled').prop(
                                'disabled', false);
                        }, 1500);
                }
            }, "json");

        }

        function showmsg(id, msg) {
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(
                ".info").html(msg);
        }
    });
</script>
{/block}
